package com.tencent.wxcloudrun.controller.zuke;

import com.tencent.wxcloudrun.config.ApiResponse;
import com.tencent.wxcloudrun.pojo.bo.ContractGenerateTask;
import com.tencent.wxcloudrun.service.business.zuke.ZuKeTodoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@RequestMapping("/zukeTodo")
public class ZuKeTodoController {

    @Resource
    private ZuKeTodoService zuKeTodoService;

    /**
     * 【租客确认】节点的处理   拒绝或同意   state传1就是同意，传2就是拒绝
     * 如果是拒绝的话，前端要弹窗让租客填写拒绝原因
     */
    @GetMapping("/rejectOrAgree")
    public ApiResponse<String> rejectOrAgree(@RequestParam("id") String id, @RequestParam("state") String state,
                                             @RequestParam("reason") String reason) throws IOException {
        return new ApiResponse<>(zuKeTodoService.rejectOrAgree(id, state, reason));
    }

    @GetMapping("/getThreeReject")
    public ApiResponse<String> getThreeReject(@RequestParam("threeId") String threeId) {
        return new ApiResponse<>(zuKeTodoService.getThreeReject(threeId));
    }

    /**
     * 预览合同 【租客确定】这个点的 id 作为入参
     * 如果合同正在生成中，返回特殊的状态码让前端显示等待提示
     */
    @GetMapping("/preview")
    public ApiResponse<String> preview(@RequestParam("id") String id) {
        String contractUrl = zuKeTodoService.preview(id);
        if (contractUrl == null) {
            // 合同正在生成中，返回特殊响应让前端显示等待提示
            return new ApiResponse<>(-1, "合同正在生成中，请稍后重试", null);
        }
        return new ApiResponse<>(contractUrl);
    }

    /**
     * 获取合同生成任务状态
     */
    @GetMapping("/getTaskStatus")
    public ApiResponse<ContractGenerateTask> getTaskStatus(@RequestParam("id") String id) {
        ContractGenerateTask task = zuKeTodoService.getTaskStatus(id);
        if (task == null) {
            return new ApiResponse<>(-1, "任务不存在", null);
        }
        return new ApiResponse<>(task);
    }
}
